package com.fh.wanandroid.ui.knowledge.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.fh.wanandroid.R;
import com.fh.wanandroid.ui.knowledge.KnowledgeDataList;
import com.fh.wanandroid.utils.CommonUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KnowledgeRvAdapter extends RecyclerView.Adapter<KnowledgeRvAdapter.MyViewHolder> {

    Context context;

    List<KnowledgeDataList> knowledgeDataList;



    //实现点击效果
    private ItemClickListener mItemClickListener;

    public KnowledgeRvAdapter(Context context,  List<KnowledgeDataList> knowledgeDataList) {

        this.context = context;

        this.knowledgeDataList = knowledgeDataList;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_knowledge_rv, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.itemKnowledgeTitle.setText(knowledgeDataList.get(position).getName());
        holder.itemKnowledgeTitle.setTextColor(CommonUtils.randomColor());

        StringBuilder content = new StringBuilder();
//        for (KnowledgeDataList data: item.getChildren()) {
//            content.append(data.getName()).append("   ");
//        }
        List<KnowledgeDataList>  childrens =  knowledgeDataList.get(position).getChildren();
        for(int i=0;i<childrens.size();i++){
            content.append( childrens.get(i).getName()).append("   ");
        }

        holder.itemKnowledgeContent.setText(content);

        setClickListener(holder, position);
    }

    private void setClickListener(MyViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null)
                    mItemClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return knowledgeDataList.size();
    }

    public List<KnowledgeDataList>  getmItems() {
        return knowledgeDataList;
    }

    public void setmItems(List<KnowledgeDataList> knowledgeDataList) {

        this.knowledgeDataList = knowledgeDataList;

        this.notifyDataSetChanged();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.item_knowledge_title)
        TextView itemKnowledgeTitle;
        @BindView(R.id.item_knowledge_content)
        TextView itemKnowledgeContent;
        @BindView(R.id.item_knowledge_group)
        CardView itemKnowledgeGroup;
        View itemView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    public ItemClickListener getmItemClickListener() {

        return mItemClickListener;
    }

    public void setmItemClickListener(ItemClickListener mItemClickListener) {

        this.mItemClickListener = mItemClickListener;
    }

    public interface ItemClickListener {
        void onClick(int position);
    }

}
